package com.lintcode.solution;

import com.lintcode.solution.entity.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class Solution_11 {
    public static void main(String[] args) {
        Solution_11 solution = new Solution_11();
        TreeNode root = new TreeNode(20);
        root.left = new TreeNode(8);
        root.right = new TreeNode(22);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(12);
        System.out.println(solution.searchRange(root, 10, 22));
    }

    /*
    * @param root: param root: The root of the binary search tree
    * @param k1: An integer
    * @param k2: An integer
    * @return: return: Return all keys that k1<=key<=k2 in ascending order
    */
    public List<Integer> searchRange(TreeNode root, int k1, int k2) {
        if (root == null) {
            return null;
        }
        List<Integer> list = new ArrayList<>();
        if (root.left != null) {
            list.addAll(searchRange(root.left, k1, k2));
        }
        if (root.val >= k1 && root.val <= k2) {
            list.add(root.val);
        }
        if (root.right != null) {
            list.addAll(searchRange(root.right, k1, k2));
        }
        return list;
    }
}
